package top.cluski.feieprint.web;

import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.cluski.feieprint.config.YouzanConfig;
import top.cluski.feieprint.domain.RespVO;
import top.cluski.feieprint.domain.dto.YouzanMatchOrderDTO;
import top.cluski.feieprint.domain.dto.YouzanShipDTO;
import top.cluski.feieprint.domain.vo.YouzanMatchOrderVO;
import top.cluski.feieprint.service.YouzanService;

@Slf4j
@RequestMapping("/youzan")
@RestController
public class YouzanController {

    @Resource
    YouzanService youzanService;

    @Resource
    YouzanConfig youzanConfig;

    @RequestMapping("/callback")
    public String matchOrder(@RequestBody JSONObject jsonObject) {
        log.info("有赞回调 {}", jsonObject.toJSONString());
        if (Boolean.TRUE.equals(youzanConfig.getWriteTeable())) {
            youzanService.callback(jsonObject, false);
        }
        return "success";
    }

    @PostMapping("/matchOrder")
    public RespVO<YouzanMatchOrderVO> matchOrder(@RequestBody YouzanMatchOrderDTO youzanMatchOrderDTO) {
        youzanService.matchOrder(youzanMatchOrderDTO);
        return RespVO.success();
    }

    @PostMapping("/confirmShip")
    public RespVO<Void> confirmShip(YouzanShipDTO youzanShipDTO) {
        youzanService.confirmShip(youzanShipDTO);
        return RespVO.success();
    }

    @PostMapping(value = "/confirmShip", headers = "x-api-version=2")
    public RespVO<Void> confirmShipV2(YouzanShipDTO youzanShipDTO) {
        youzanService.confirmShipV2(youzanShipDTO);
        return RespVO.success();
    }

}
